//
//  TSYOAuthView.swift
//  TSYWeiBoDemo
//
//  Created by apple on 2016/11/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit


class TSYOAuthView:UIViewController{
    
    private lazy var webView = UIWebView()
    
    override func loadView() {
        
        view = webView
        
        title = " denglu"
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", fontSize: 12, target: self, action: #selector(back), isBack: true)
        
    }
    
    override func viewDidLoad() {
        let urlString = "www.baidu.com"
        
        
        
        guard let url = URL(string: urlString) else {
            return
        }
        
        let request = URLRequest(url: url)
        
        webView.loadRequest(request)
        
        webView.delegate = self
    }
    
    
    @objc private func back(){
        
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func autoFill(){
        
        let js = "document.getElementById('userId').value = 'taosiyu';"+"document.getElementById('passwd').value = '123456'"
        
        webView.stringByEvaluatingJavaScript(from: js)
    }
    
}

extension TSYOAuthView:UIWebViewDelegate{
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
         print("hahah = \(request.url?.absoluteString)")
        
        
        if request.url?.absoluteString.hasPrefix("www.baidu.com") == false {
            
            return true
        }
    
        if request.url?.query?.hasPrefix("code=") == false{
            
            print("不包含返回码，取消")
            
            dismiss(animated: true, completion: nil)
        }
        
        
        return true
    }
    
}



